<?php

namespace App\Http\Controllers\Home;
use Gregwar\Captcha\CaptchaBuilder;
use Hash;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class LoginController extends Controller
{
    //前台登录
	public function getIndex()
	{
		return view('Home.login.index');
	}
    //处理登录
	public function postDologin(Request $request)
    {
        //根据用户名查询
        //dd($request->all());
        $user = DB::table('user')->where('username',$request->input('username'))->first();
        //dd($user); 
        if(empty($user)){
            return back()->with('error','请输入用户名或密码');
        }
        //判断密码和库里是否一样
        if(Hash::check($request->input('password'),$user->password)){
            //登录成功
            session(['userid'=>$user->id]);
            // dd(session()->all());
            $userid=session('userid');
            $hh=DB::table('userdetail')->where('id',$userid)->first();
            $username=$hh->username;
            session(['username'=>$username]);
            // dd(session('username'));
            // $request->session()->put('id', $user->id);
           // \Session::save();
            return redirect('/home/')->with('success','登陆成功');
        }else{
            return back()->with('error','用户名或密码不正确');
        }
    }
     
         public function vcode()
    {
        ob_clean();//清除输出缓存区的内容 
        //生成验证码图片的Builder对象，配置相应属性 
        $builder = new CaptchaBuilder; 
        //可以设置图片宽高及字体 
        $builder->build($width = 100, $height = 40, $font = null); 
        //获取验证码的内容 
        $phrase = $builder->getPhrase(); 
        //把内容存入session 
        session(['Vcode'=>$phrase]); 

        //生成图片 
        header("Cache-Control: no-cache, must-revalidate"); 
        header('Content-Type: image/jpeg'); $builder->output(); 
        //看到图片输出 需要
       // die; //模板中使用 注意路由 

    }
}
